import io from "socket.io-client";
const socket = io("http://localhost:5008");
import useUsersStore from "@/store/useUsersStore";

socket.on("connect", () => {
  console.log("Connected to server");
  // socket.emit("joinRoom", { room: "room1" });
});

socket.on("userin", data => {
  const usersStore = useUsersStore();
  console.log("userin", data);
  usersStore.addUser(data);
})

socket.on("userout", data => {
  const usersStore = useUsersStore();
  console.log("userout", data);
  usersStore.deleteUser(data.id);
})

// socket.on("message", (data) => {
//   console.log(data);
//   console.log("test", data.msg.includes("http"));
//   console.log("test2", data.msg.split("\""));
//   let msg = data.msg;
//   if (
//     import.meta.env.VITE_ENV === "development" &&
//     data.msg.includes("img") &&
//     !data.msg.includes("http")
//   ) {
//     const arr = data.msg.split("/resources")
//     arr.splice(1,0,"http://localhost:5008/resources")
//     msg = arr.join("")
//   }
//   const usersStore = useUsersStore();
//   usersStore.addTexts({
//     id: data.id,
//     msg: { isSelf: false, text: msg },
//   });
// });

export default socket;
